<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\CommonModel;
use app\admin\model\VideoCategoryModel;
use cmf\controller\AdminBaseController;
use think\Db;

//视频分类
class VideoCategoryController extends AdminBaseController
{
    public function index(){
        $list = Db::name('video_category')
            ->field('*')
            ->order('id desc')
            ->paginate(null,false,['query'=>$this->request->param()]);
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    //添加页面
    public function add(){
        return $this->fetch();
    }

    //添加提交
    public function addPost(){
        $data = $this->request->param();
        $result = $this->validate($data,'VideoCategory.add');
        if($result !== true){
            $this->error($result);
        }
        //判断分类是否存在
        $commonModel = new CommonModel();
        $is_category = $commonModel->findWhereData('video_category',['video_name'=>$data['video_name']],'id');
        if($is_category){
            $this->error('该分类已存在！');
        }

        //过滤data数组中的非数据表字段数据
        $videoCategoryModel = new VideoCategoryModel();
        $res = $videoCategoryModel->allowField(true)->create($data);
        if($res){
            $this->success('添加成功!', url('VideoCategory/index'));
        }else{
            $this->error('添加失败');
        }
    }

    //编辑页面
    public function edit(){
        $id = $this->request->param('id');
        $post = Db::name('video_category')
            ->where(['id'=>$id])
            ->find();
        $this->assign('post',$post);
        return $this->fetch();
    }

    //编辑提交
    public function editPost(){
        $data = $this->request->param();
        $result = $this->validate($data,'VideoCategory.edit');
        if($result !== true){
            $this->error($result);
        }
        //判断分类是否存在
        $commonModel = new CommonModel();
        $where[] = ['id','<>',$data['id']];
        $where[] = ['video_name','=',$data['video_name']];
        $is_category = $commonModel->findWhereData('video_category',$where,'id');
        if($is_category){
            $this->error('该分类已存在！');
        }

        //过滤data数组中的非数据表字段数据
        $videoCategoryModel = new VideoCategoryModel();
        $res = $videoCategoryModel->allowField(true)->save($data,['id'=>$data['id']]);
        if($res){
            $this->success('保存成功!', url('VideoCategory/index'));
        }else{
            $this->error('保存失败');
        }
    }

    //删除
    public function delete(){
        $id = $this->request->param('id');
        //查询视频管理
        $commonModel = new CommonModel();
        $res_video = $commonModel->findWhereData('video',['video_category_id'=>$id],'id');
        if($res_video){
            $this->error('该分类下存在视频，不能删除！');
        }
        $videoCategoryModel = new VideoCategoryModel();
        $res = $videoCategoryModel->where('id',$id)->delete();
        if($res){
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
}